package game.targets;

import game.targets.device.Device;
import game.targets.device.DevicePC;
import game.targets.entity.Assets;
import game.targets.screen.ScreenHandler;
import game.targets.sound.IAudio;
import game.targets.sound.NullAudio;
import game.targets.sound.Sounds;

import java.util.ArrayList;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputMultiplexer;
import com.badlogic.gdx.Preferences;
import com.badlogic.gdx.graphics.g2d.ParticleEffect;
import com.badlogic.gdx.physics.bullet.dynamics.btDiscreteDynamicsWorld;
import com.badlogic.gdx.physics.bullet.linearmath.btIDebugDraw.DebugDrawModes;

public class Locator implements Constants {
	private static IAudio audioService;
	private static NullAudio nullService;
	private static ArrayList<ParticleEffect> effects;
	private static InputMultiplexer multi;
	private static Device device;
	private static Game game;
	private static int maxLevel;
	private static int level;
	private static Assets assets;
	private static Sounds sounds;
	private static btDiscreteDynamicsWorld world;
	private static DebugDraw drawer;
	private static ScreenHandler screenHandler;
	private static Stats stats;

	public static void init(Game g, int maxL) {
		nullService = new NullAudio();
		audioService = nullService;
		effects = new ArrayList<ParticleEffect>();
		multi = new InputMultiplexer();
		game = g;
		level=maxL;
		maxLevel=maxL;
		stats=new Stats();
		
		assets=new Assets();
		assets.loadAssets();
		
		sounds=new Sounds();
	}
	
	public static void provideHandler(ScreenHandler sh){
		screenHandler=sh;
	}

	public static void win() {
		writeSave();
		audioService.stopMusic();
	}

	public static void writeSave() {
		Preferences prefs = Gdx.app.getPreferences("My Preferences");
		prefs.putInteger("MaxLevel", maxLevel);
		prefs.flush();
	}
	
	public static void provideWorld(btDiscreteDynamicsWorld w){
		world=w;
		drawer=new DebugDraw();
		drawer.setDebugMode(DebugDrawModes.DBG_DrawWireframe);
		world.setDebugDrawer(drawer);
	}
	
	public static void nextLevel(){
	    increaseLevel();
	    Locator.writeSave();
	}
	
	public static void increaseLevel(){
	    level++;
	    if(level > maxLevel){
	        maxLevel++;
	    }
	}
	
	public static DebugDraw getDebug(){
	    return drawer;
	}

	public static void provideDevice(Device d) {
		if (d == null) {
			device = new DevicePC();
		}
		device = d;
	}

	public static void provideAudio(IAudio service) {
		if (service == null) {
			// use null audio
			audioService = nullService;
		} else {
			audioService = service;
		}
	}
	
	public static Stats getStats() {
		return stats;
	}

	public static void setStats(Stats stats) {
		Locator.stats = stats;
	}

	public static btDiscreteDynamicsWorld getWorld(){
		return Locator.world;
	}
	
	public static Sounds getSounds(){
		return Locator.sounds;
	}
	
	public static Assets getAssets(){
		return Locator.assets;
	}

	public static ScreenHandler getScreenHandler() {
		return screenHandler;
	}

	public static void setScreenHandler(ScreenHandler screenHandler) {
		Locator.screenHandler = screenHandler;
	}

	public static int getLevel() {
        return level;
    }

    public static void setLevel(int level) {
        Locator.level = level;
    }

    public static int getMaxLevel() {
        return maxLevel;
    }

    public static void setMaxLevel(int maxLevel) {
        Locator.maxLevel = maxLevel;
    }

    public static void clearData() {
    	maxLevel=level=1;
	}

	public static InputMultiplexer getInputMultiplexer() {
		return multi;
	}

	public static IAudio getAudio() {
		return audioService;
	}

	public static ArrayList<ParticleEffect> getEffects() {
		return effects;
	}

	public static Device getDevice() {
		return device;
	}

	public static Game getGame() {
		return game;
	}

}
